package com.lemner.ke.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemner.ke.R;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.TopBar;
import com.lemner.ke.bean.SuccessBean;
import com.lemner.ke.model.persional.ResetMobileModel;
import com.lemner.ke.util.Phone;
import com.lemner.ke.util.SpUtils;

/**

* 作者：张恺

* 时间：2018/9/18

* 类描述：更换手机号页面

*/

public class ChangePhoneActivity extends BaseActivity {
    private ImageView iv_eye;
    private Boolean eyeOpen = false;
    private EditText et_phone,et_password;
    private TextView tv_forget;
    private Button btn_binding;
    @Override
    public int getLayoutFile() {
        return R.layout.activity_change_phone;
    }

    @Override
    public void initView() {
        iv_eye=findViewById(R.id.iv_change_phone_eye);
        et_phone=findViewById(R.id.et_change_phone_phone);
        et_password=findViewById(R.id.et_change_phone_password);
        tv_forget=findViewById(R.id.tv_change_phone_forget);
        btn_binding=findViewById(R.id.btn_change_phone_binding);
    }

    @Override
    public void initEvent() {
        iv_eye.setOnClickListener(this);
        tv_forget.setOnClickListener(this);
        btn_binding.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initSetting() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_change_phone_eye:
                if (eyeOpen == false) {
                    iv_eye.setImageDrawable(getResources().getDrawable(R.drawable.change_phone_eye_open));
                    et_password.setInputType(1);
                    eyeOpen = true;
                } else if (eyeOpen == true) {
                    iv_eye.setImageDrawable(getResources().getDrawable(R.drawable.change_phone_eye_close));
                    et_password.setInputType(129);
                    eyeOpen = false;
                }
                break;
            case R.id.btn_change_phone_binding:
                final String mobile = et_phone.getText().toString();
                final String password = et_password.getText().toString();
                if (mobile.isEmpty()) {
                    showToast("请输入手机号！");
                } else if (password.isEmpty()) {
                    showToast("请输入密码！");
                } else if (!Phone.isMobile(et_phone.getText().toString())) {
                    showToast("非法手机号，请重新输入！");
                } else if (password.length() < 6) {
                    showToast("密码长度不能少于6位！");
                } else {
                    new ResetMobileModel().reserMobile(SpUtils.getString(this, SpUtils.USERIDS), mobile, new BaseListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getCode().equals("0000")) {
                                showToast(successBean.getMsg());
                            } else {
                                showToast(successBean.getMsg());
                                startActivity(AccountSecurityActivity.class);
                                finish();
                            }
                        }

                        @Override
                        public void onFail(String msg) {

                        }
                    });
                }
                break;
            case R.id.tv_change_phone_forget:
                startActivity(ForgetPasswordActivity.class);
                break;
        }
    }
    }
